### Male

male <- survey::svyby(~Pl10, ~M1, srs_treatment, svymean, na = TRUE)

male <- as.data.table(male)

male[, variable := "Male"]

male <- male[, .(variable, ind = M1, Pl10, se)]

### Young person (< 24 yo)

young <- svyby(~Pl10, ~young, srs_treatment, svymean, na = TRUE)

young <- as.data.table(young)

young[, variable := "Young"]

young <- young[, .(variable, ind = young, Pl10, se)]

### High economic literacy

highknowl <- svyby(~Pl10, ~M6, srs_treatment, svymean, na = TRUE)

highknowl <- as.data.table(highknowl)

highknowl[, variable := "High economic literacy"]

highknowl <- highknowl[, .(variable, ind = highknowledge, Pl10, se)]

### City

city <- svyby(~Pl10, ~city, srs_treatment, svymean, na = TRUE)

city <- as.data.table(city)

city[, variable := "City"]

city <- city[, .(variable, ind = city, Pl10, se)]

### Higher education

highedu <- svyby(~Pl10, ~higheredu, srs_treatment, svymean, na = TRUE)

highedu <- as.data.table(highedu)

highedu[, variable := "Higher education"]

highedu <- highedu[, .(variable, ind = higheredu, Pl10, se)]

### High income

highinc <- svyby(~Pl10, ~highincome, srs_treatment, svymean, na = TRUE)

highinc <- as.data.table(highinc)

highinc[, variable := "High income"]

highinc <- highinc[, .(variable, ind = highincome, Pl10, se)]

### Pariticiption in elections

partel <- svyby(~Pl10, ~M17, srs_treatment, svymean, na = TRUE)

partel <- as.data.table(partel)

partel[, variable := "Participation in elections"]

partel <- partel[, .(variable, ind = M17, Pl10, se)]

### Support for government

supgov <- svyby(~Pl10, ~M_20, srs_control, svymean, na = TRUE)

supgov <- as.data.table(supgov)

supgov[, variable := "Support for government"]

supgov <- supgov[, .(variable, ind = M_20, Pl10, se)]

### Unemployed

unemp <- svyby(~Pl10, ~M11_3_b, srs_treatment, svymean, na = TRUE) 

unemp <- as.data.table(unemp)

unemp[, variable := "Unemployed"]

unemp <- unemp[, .(variable, ind = M11_3_b, Pl10, se)]

### Self-employed

selfemp <- svyby(~Pl10, ~M12_2_b, srs_treatment, svymean, na = TRUE) 

selfemp <- as.data.table(selfemp)

selfemp[, variable := "Self-employed"]

selfemp <- selfemp[, .(variable, ind = M12_2_b, Pl10, se)]

### Employed

emp <- svyby(~Pl10, ~M11_1_b, srs_treatment, svymean, na = TRUE) 

emp <- as.data.table(emp)

emp[, variable := "Employed"]

emp <- emp[, .(variable, ind = M11_1_b, Pl10, se)]

### Student

student <- svyby(~Pl10, ~M11_5_b, srs_treatment, svymean, na = TRUE) 

student <- as.data.table(student)

student[, variable := "Student"]

student <- student[, .(variable, ind = M11_5_b, Pl10, se)]

### Bind all 

Pl10_cov <- rbind(male,young, highknowl, city, highedu, highinc, partel, supgov, unemp, selfemp, emp, student)

Pl10_cov <- as.data.table(Pl10_cov)

Pl10_cov[, ind := ifelse(ind == "1", "Yes", "No")]

Pl10_cov <- ggplot(Pl10_cov, aes(x = Pl10, y = variable, color = factor(ind))) +
  geom_point(position = position_dodge(width = 0.5), size = 2) + theme_light() + scale_fill_brewer(palette = "Spectral") +
  geom_errorbarh(aes(xmin = Pl10 - se, xmax = Pl10 + se), position = position_dodge(width = 0.5), height = 0.1) +
  scale_color_manual(values = c("darkgreen", "darkred")) + 
  theme(axis.text.x = element_text(color = "grey20", size = 18, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        axis.text.y = element_text(color = "grey20", size = 18, angle = 0, hjust = 1, vjust = 0, face = "plain"),  
        axis.title.x = element_text(color = "grey20", size = 18, angle = 0, hjust = .5, vjust = 0, face = "plain"),
        axis.title.y = element_text(color = "grey20", size = 18, angle = 90, hjust = .5, vjust = .5, face = "plain"),
        legend.text = element_text(color = "grey20", size = 18, hjust = .5, vjust = .5, face = "plain"),
        legend.title = element_text(color = "grey20", size = 18, face = "plain"),
        legend.position="bottom",
        legend.background = element_blank(),
        legend.box.background = element_rect(colour = "grey20")) +
  labs(x = "Share of spending wasted (%)", y = "", color='Indicator')

ggsave("Pl10_cov_treatment.jpeg", device='jpeg', dpi=900)
